<?php
require_once ('include/css_head.php');
require_once ('include/functions.php');
require_once ('include/header.php');
require_once ('../BL/BL.php');
require_once ('../Frameworks/Objects/entity.php');


if ( isset($_GET['id']) )
{
    if ( !ctype_digit((string)$_GET['id']) ) {
        UI_registerError('msg_error', "Invalid category request");
        header("location: index.php");
        exit(0);
    }
}
else
{
    UI_registerError('msg_warning', "You haven't selected a category to select entries from!");
    UI_registerError('msg_warning', "Please select a category from the sidebar to the left.");
    header("location: index.php");
    exit(0);
}

$category = BL_getCategory($_GET['id']);
$attributes = $category->GetAttributes();
$result = BL_selectCategory($_GET['id']);

function printData(&$entity)
{
    echo "<h3 style='margin-left: 20px;'>Attributes</h3>";
    $values = $entity->GetAttributes();
    echo "<div class='block blockRed' style='margin-left: 20px; border: gray solid 1px;'>";
    echo "<table>";
    for ($i = 0; $i < count($values); $i++) {
        echo "<tr>";
        echo "<td style='text-align:right;color:blue;'>".$values[$i]->getName()." : </td><td>".$values[$i]->getValue()."</td>";
        echo "</tr>";
    }
    echo "</table>";
    echo "</div>";
}
?>
<div style="width: 245px;" id="sidebar"></div>
<div id="content">
<h3 class="blockHeader" style="margin-bottom:5px; width: 400px">Category: <span style='color: red'><?php echo $category->getName() ?></span></h3>


 <table class="blockBlue" style="padding: 5px; border:1px solid black; margin-bottom: 20px">
                    <tr>
                        <th class='tC tCC'  style="font-weight:bold;">Attributes </th>

                        <th class='tC tCC'   style="font-weight:bold;">Filters</th>
                    </tr>

 <?php
$i = 0;
foreach ($attributes as $attribute) {
?>
<tr>
    <td class='tC'><?php echo $attribute->GetName(); ?></td>
	<td class='tC'>contains : <input type="text" name="filter" /></td>
</tr>
<?php
	$i++;
	}
?>
 </table>
<button id="filter_button" type="button" >Filter</button> <br/> <br/>

<form style='inline-block' id="submitEntities" name="submitEntities" action="weights.php?id=<?php echo $category->getId() ?>" method="post">
<?php
for ($i = 0; $i < count($result); $i++) {
?>
    <div style="float:left; width:100%">

        <div style="float:left; width:2%; padding-top: 1px">
            <input style="margin-left: 6px; " name="entities[]" type="checkbox" value=" <?php echo $result[$i]->getId() ?>" checked="checked"/> </div>

        <div style="float:left; width: 20px" ><img id="<?php echo $result[$i]->getId() ?>"
            class="expandable" src="images/arrow-right.gif" onclick="changeSrc(this);" /></div>

        <div style="width:95%; float:left; padding:2px 0 2px 0"> <?php echo $result[$i]->getname() ?></div>

        <div style="float:left" id="<?php echo "collapse" . $result[$i]->getId() ?>" class="collapse" >
                <?php printData($result[$i]) ?>
        </div>

    </div>
<?php
}
?>

<span style='font-size: 0.75em;'>


<a href='#' onclick='javascript:checkAll()' style="text-decoration:none;color:blue;">(check all)</a> - <a href='#' onclick='javascript:uncheckAll()' style="text-decoration:none;color:blue;">(uncheck all)</a>
</span>
<input style="margin-top: 20px" type="button" value="Submit" onclick="javascript:prepareChecked()"/>
<input type="hidden" name="entity_ids" />
</form>
</div>


<script type="text/javascript">
        $.ajaxSetup ({ cache: false });
        var ajax_load = "<img src='images/load.gif' alt='loading...' />";
        $("#sidebar").load($("#sidebar").html(ajax_load).load('sidebar.php'));
</script>

<script type="text/javascript">
    $(document).ready(function() {
        $('.collapse').hide();

        $('img.expandable').click(function() {
             var id = $(this).attr('id');
			 $('#collapse' + id).slideToggle();
		
		
			 return false;
		  });
		  
		/*--- filter code ---*/
		$('input[name="filter"]').change(function() {
		   $('#filter_button').text("Filter(*)");
		});
		$('#filter_button').click(function() {
			$('#filter_button').text("Filter");
			
			$('input[name="filter"]').each(function(index1){
				var cattrname = $(this).parent().prev().text();
				var keyword = $(this).attr('value');
				var boxes = document.getElementsByName("entities[]");
				
				if(keyword != ""){
					$('div.block td:contains(' + cattrname + ')').each(function(index2){
						
						var cattrval = $(this).next().text();
						
						
						var rg = new RegExp(keyword,'i');
						if(cattrval.search(rg) >= 0){
							boxes[index2].checked = true;
						}
						else{
							boxes[index2].checked = false;
						}
					
					});
				}
				
				
				
			});
		  
		});
		
		/* -----------*/
});



function checkAll()
{
    var boxes = document.getElementsByName("entities[]");

    for ( var i = 0 ; i < boxes.length ; i++ )
         boxes[i].checked = true;
}

function uncheckAll()
{
    var boxes = document.getElementsByName("entities[]");

    for ( var i = 0 ; i < boxes.length ; i++ )
         boxes[i].checked = false;
}

function changeSrc(obj)
{
    var path=obj.src;
    var zName = path.substring(path.lastIndexOf("-")+1, path.lastIndexOf("."));
    if(zName == "right")
        obj.src = path.replace(/right/gi, "down");
    else
        obj.src = path.replace(/down/gi, "right");
}

function prepareChecked()
{
    var mat = new Array();

    var entities = document.getElementsByName("entities[]");

    for ( var i = 0 ; i < entities.length ; i ++ ) {
        if ( entities[i].checked == true )
            mat.push(entities[i].value);
    }

    document.submitEntities.entity_ids.value = mat;
    document.submitEntities.submit();
}
</script>
<?php
require_once ('include/footer.php');
?>